/********************************************************************************
 * Project Name		[JavaSE_Fundamentals]
 * File Name     	[Class.java]
 * Creation Date 	[2015-01-01]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ********************************************************************************/
package objectOriented;

import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 定义一个类<br/>
 * 
 * new 关键字在生成对象时 了三件事情<br>
 * 为对象分配内存空间<br>
 * 调用类的构造方法<br>
 * 将生成对象的地址返回<br>
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 * 
 */
public class Class {

	public static void main(String[] args) {
		Employee[] staff = new Employee[3];
		staff[0] = new Employee("Sean Yang", 75000, 2011, 6, 20);
		staff[1] = new Employee("Ge Yang", 75000, 2011, 6, 20);
		staff[2] = new Employee("anonymous", 100000, 2000, 1, 1);

		for (Employee employee : staff) {
			employee.raiseSalary(10);
		}

		for (Employee employee : staff) {
			System.out.println(employee);
		}
	}
}

/**
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 */
class Employee {
	// 属性定义
	private final String name;
	private double salary;
	private Date hireDate;

	// 自动生成ID
	private static int nextId = 1;
	private int id;

	/**
	 * 构造方法
	 */
	public Employee() {
		super();
		this.name = "";
	}

	/**
	 * 构造方法
	 * 
	 * @param name
	 * @param salary
	 * @param year
	 * @param month
	 * @param day
	 */
	public Employee(String name, double salary, int year, int month, int day) {
		super();
		this.name = name;
		this.salary = salary;
		GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
		this.hireDate = calendar.getTime();
		this.setId();
	}

	// 方法定义
	public int getId() {
		return this.id;
	}

	private void setId() {
		this.id = nextId++;
	}

	public String getName() {
		return name;
	}

	public double getSalary() {
		return salary;
	}

	public void setSalary(double salary) {
		this.salary = salary;
	}

	public Date getHireDate() {
		// 一般情况下如果返回的一个可变对象的引用, 那么应该对其使用clone操作
		return (Date) hireDate.clone();
	}

	public void setHireDate(Date hireDate) {
		this.hireDate = hireDate;
	}

	public void raiseSalary(double byPercent) {
		salary += salary * byPercent / 100;
	}

	@Override
	public String toString() {
		return "Id " + this.id + "\tname " + this.name + "\tsalary " + this.salary + "\thiredate " + this.hireDate;
	}
}